/*
* Timer.c
*/

#include <Windows.h>
#include <CommCtrl.h>
#include <tchar.h>
#include "resource.h"
#include "tiny-timer.h"

// Necessary to get the new styles

#pragma comment(linker, \
	"\"/manifestdependency:type='Win32' "\
	"name='Microsoft.Windows.Common-Controls' "\
	"version='6.0.0.0' "\
	"processorArchitecture='*' "\
	"publicKeyToken='6595b64144ccf1df' "\
	"language='*'\"")

#pragma comment(lib, "ComCtl32.lib")

/* =================================================== */

const int nSecondsMax	= 5;
int nSecondsRemaining	= -1;
BOOL bComplete			= FALSE;
char chBuffer[120];

#define IDT_TIMER1		101

void Pomodoro_FormatTime(LPTSTR chBuffer, int t)
{
	wsprintf(chBuffer, L"%02d:%02d", t / 60, t % 60);
}

void Pomodoro_Reset(HWND hDlg)
{
	bComplete = FALSE;
	nSecondsRemaining = nSecondsMax;
	Pomodoro_Update(hDlg, nSecondsRemaining);
}

void Pomodoro_Update(HWND hDlg, int t)
{
	Pomodoro_FormatTime((LPTSTR)chBuffer, t);

	// this sets the title:
	SetWindowTextW(hDlg, (LPTSTR)chBuffer);
}

void Pomodoro_Tick(HWND hDlg)
{
	if(--nSecondsRemaining < 1) {
		nSecondsRemaining = 0;
		bComplete = TRUE;
	}
	Pomodoro_Update(hDlg, nSecondsRemaining);
}

/* =================================================== */

INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT rec;
	switch(uMsg)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case ID_BTN_START:
			SetTimer(hDlg, IDT_TIMER1, 1000, (TIMERPROC) NULL);
			return TRUE;
		case ID_BTN_STOP:
			KillTimer(hDlg, IDT_TIMER1);
			return TRUE;
		case ID_BTN_RESET:
			KillTimer(hDlg, IDT_TIMER1);
			Pomodoro_Reset(hDlg);
			SetRect(&rec,10,10,200,100);
			InvalidateRect(hDlg, &rec, TRUE);
			return TRUE;
		}
		break;
	case WM_ERASEBKGND:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hDlg, &ps);
			RECT rec;
			GetClientRect(hDlg, &rec);
			FillRect(hdc, &rec, GetSysColorBrush(COLOR_WINDOW));
			SetRect(&rec, 10, 10, 200, 100);
			DrawText(hdc, (LPCTSTR)chBuffer,120, &rec, DT_LEFT|DT_NOCLIP);
			EndPaint(hDlg, &ps);
			ReleaseDC(hDlg, hdc);
		}
		return (LRESULT)1;
	case WM_TIMER:
		{
			Pomodoro_Tick(hDlg);
			SetRect(&rec, 10, 10, 200, 100);
			InvalidateRect(hDlg, &rec, TRUE);
			if(bComplete) {
				KillTimer(hDlg, IDT_TIMER1);
				FlashWindow(hDlg, TRUE);
				MessageBeep(0xFFFFFFFF);
				MessageBox(hDlg, TEXT("Timer Complete"), TEXT("Timer"), MB_OK);
				// TODO: Disable the Start button
			}
		}
		break;
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hDlg, &ps);
			RECT rec;
			SetRect(&rec,10,10,200,100);
			DrawText(hdc, (LPCTSTR)chBuffer,120, &rec, DT_LEFT|DT_NOCLIP);
			EndPaint(hDlg, &ps);
			ReleaseDC(hDlg, hdc);
		}
		break;
	case WM_CLOSE:
		if(MessageBox(hDlg, TEXT("Close the program?"), TEXT("Close"), MB_ICONQUESTION | MB_YESNO) == IDYES) {
			KillTimer(hDlg, IDT_TIMER1);
			DestroyWindow(hDlg); // modeless, otherwise EndDialog
		}
		return TRUE;

	case WM_DESTROY:
		PostQuitMessage(0);
		return TRUE;
	}

	return FALSE;
}

int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR lpCmdLine, int nCmdShow)
{
	HWND hDlg;
	MSG msg;
	BOOL ret;
	HICON hIcon;

	// Initialize resources
	Pomodoro_FormatTime((LPTSTR)chBuffer, nSecondsMax);

	hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APP_ICON));

	InitCommonControls();

	// Use a modeless dialog as the main application window
	hDlg = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_MAIN_DIALOG), 0, DialogProc, 0);

	ShowWindow(hDlg, nCmdShow);

	SendMessageW(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);

	Pomodoro_Reset(hDlg);

	while((ret = GetMessage(&msg, 0, 0, 0)) != 0)
	{
		if(ret == -1)
			return -1;
		if(!IsDialogMessage(hDlg, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return 0;
}